﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Marmat.Forms.Skin
{
    /// <summary>
    /// 面包屑控件
    /// </summary>
    [ToolboxItem(true)]
    public partial class Breadcrumb : Control, ISkinControl
    {
        /// <summary>
        /// 
        /// </summary>
        public Breadcrumb()
        {
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            base.BackColor = Color.Transparent;
            this.Items = new BreadcrumbItemCollection(this);
        }

        /// <summary>
        /// 获取子项的父级，返回值为null则表示未查到父级
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public BreadcrumbItem GetItemParent(BreadcrumbItem item)
        {
            BreadcrumbItem recult = null;
            if (this.Items.Contains(item))
            {
                int index = this.Items.IndexOf(item);
                if (index != 0)
                {
                    recult = this.Items[index - 1];
                }
            }

            return recult;
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.rectangleDec.Clear();
            e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            if (this.BackColor != Color.Transparent)//只有颜色不为透明时绘制
            {
                using (var bush = new SolidBrush(this.BackColor))
                    e.Graphics.FillRectangle(bush, e.ClipRectangle);
            }
            int width = 4;
            foreach (var item in this.Items)
            {
                string text = string.IsNullOrEmpty(item.Text) ? item.Name : item.Text;
                var font_size = TextRenderer.MeasureText(text, this.Font);
                var y = (this.Height - font_size.Height) / 2;
                var font_rect = new Rectangle(width, y, font_size.Width, font_size.Height);
                TextRenderer.DrawText(e.Graphics, text, this.Font, font_rect, GetFouseColor(font_rect, item), TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
                this.rectangleDec[item] = font_rect;
                width += font_rect.Width;
                if (!this.Items.IsLast(item))
                {

                    var size = TextRenderer.MeasureText(this.IntervalChar.ToString(), this.Font);
                    var rec_Interval = new Rectangle(width, y, size.Width, size.Height);
                    TextRenderer.DrawText(e.Graphics, this.IntervalChar.ToString(), this.Font, rec_Interval, Color.Gray, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
                    width += rec_Interval.Width;
                }
            }
        }

        private Color GetFouseColor(Rectangle rec, BreadcrumbItem button)
        {
            Color back_color = this.ForeColor;
            if (button.AllowClick)
            {
                if (!this.Enabled)
                    back_color = this.ForeColor.Blend(Color.FromArgb(Convert.ToInt32(256 * 0.5), Color.White));
                else if (rec.Contains(point) && this.clickItem == button)
                    back_color = this.SelectedColor.Blend(Color.FromArgb(Convert.ToInt32(255 * 0.1), Color.Black));
                else if (rec.Contains(point))
                    back_color = this.SelectedColor.Blend(Color.FromArgb(Convert.ToInt32(255 * 0.2), Color.White));
            }
            return back_color;
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            point = e.Location;
            this.Invalidate();
            base.OnMouseMove(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            point.X = -1;
            point.Y = -1;
            this.Invalidate();
            base.OnMouseLeave(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                foreach (var recItme in rectangleDec)
                {
                    if (recItme.Value.Contains(e.Location))
                    {
                        if (recItme.Key.AllowClick)
                        {
                            this.clickItem = recItme.Key;
                            this.Invalidate();
                            return;
                        }
                    }
                }
            }
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            this.clickItem = null;
            if (e.Button == MouseButtons.Left)
            {
                foreach (var recItme in rectangleDec)
                {
                    if (recItme.Value.Contains(e.Location))
                    {
                        if (recItme.Key.AllowClick)
                        {
                            var args = new BreadcrumbArgs(recItme.Key);
                            this.ItemClicked?.Invoke(this, args);
                            this.Invalidate();
                            return;
                        }
                    }
                }
            }
            base.OnMouseUp(e);
        }
    }
}
